<template>
	<view class="wrap">
		<view class="main" v-if="hasLoad">
			<view class="user-data" :style="'background-image:url(' + topImage + ')'">
				<view class="user-box">
					<view class="user-info">
						<view class="b-avatar">
							<image :src="userInfo.avatar_url || '/static/imgs/user.png'" mode="aspectFill"></image>
						</view>
						<view class="b-info">
							<view class="s-name">{{userInfo.name}}</view>
							<view class="s-org">{{userInfo.group_name}}</view>
						</view>
					</view>
					<view class="b-action" @click="onJumpOrder">
						<view class="s-btn">兑换订单</view>
					</view>
				</view>
				<view class="fund-box">
					<view class="b-fund">
						<view class="s-tit">剩余</view>
						<view class="s-num">{{fundInfo.amount_left}}</view>
						<view class="s-unit">礼币</view>
					</view>
					<view class="b-action" @click="onJumpRecord">
						<view class="s-btn">礼币记录</view>
					</view>
				</view>
			</view>
			<view class="hot-gifts">
				<view class="b-column">热门兑换</view>
				<scroll-view scroll-x class="b-items" v-if="hotList.length > 0">
					<view class="b-item" v-for="(item, index) in hotList" :key="index" @click="onJumpGift(item.id)">
						<view class="b-main">
							<view class="b-image">
								<image :src="item.cover_url" mode="aspectFill"></image>
								<view class="s-gain">已兑{{item.count_gain}}</view>
							</view>
							<text class="s-title too-long">{{item.name}}</text>
							<view class="s-fund">{{item.amount}}礼币</view>
							<view class="s-action" v-if="item.count_left > 0">兑换</view>
							<view class="s-action f-disable" v-else>已兑完</view>
						</view>
					</view>
				</scroll-view>
			</view>
			<view class="all-gifts">
				<view class="b-column">
					<view class="s-tit">全部礼品</view>
					<view class="s-more" @click="onJumpAll">
						<view class="s-cont">更多</view>
						<view class="iconfont icon-more"></view>
					</view>
				</view>
				<view class="b-items" v-if="allList.length > 0">
					<view class="b-item" v-for="(item, index) in allList" :key="index" @click="onJumpGift(item.id)">
						<view class="b-left">
							<image :src="item.cover_url" mode="aspectFill"></image>
						</view>
						<view class="b-main">
							<view class="b-title too-long">{{item.name}}</view>
							<view class="b-center">
								<view class="b-fund">
									<view class="s-num">{{item.amount}}礼币</view>
									<view class="s-worth" v-if="item.worth > 0">价值 {{item.worth}}元</view>
								</view>
								<view class="b-action">
									<view class="s-action" v-if="item.count_left > 0">兑换</view>
									<view class="s-action f-disable" v-else>已兑完</view>
								</view>
							</view>
							<view class="b-amount">
								<view class="s-left">库存 {{item.count_left}}</view>
								<view class="s-gain">已兑 {{item.count_gain}}</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 加载中 -->
		<load-more :loadingType="loadingType" :loadingText="loadingText" :top="300"></load-more>
		<!-- 上传头像 -->
		<upload-avatar :show="display.avatarChange" :image="userInfo.avatar_url" @bindClose="onAvatarClose"
			@bindChange="onAvatarChange"></upload-avatar>
	</view>
</template>

<script>
	import {
		getHomeData
	} from '@/service/api/page.js'
	import mixinsCommon from '@/mixins/common.js'
	import mixinsAuth from '@/mixins/auth.js'
	export default {
		mixins: [mixinsCommon, mixinsAuth],
		data() {
			return {
				userInfo: {},
				fundInfo: {},
				hotList: [],
				allList: [],
				hasLoad: false,
				loadingType: 1,
				loadingText: '',
				display: {
					avatarChange: false
				}
			}
		},
		onShow() {
			this.loadHomeData()
		},
		computed: {
			topImage() {
				return this.$store.getters.globalConfig.home_top_img || '/static/imgs/home_top_bg.png'
			}
		},
		methods: {
			loadHomeData() {
				getHomeData().then(([err, res]) => {
					console.log('getHomeData', err, res)
					if (!err) {
						this.hasLoad = true
						this.loadingType = -1
						this.userInfo = res.userInfo
						this.fundInfo = res.fundInfo
						this.hotList = res.hotList
						this.allList = res.giftList
						if (!this.userInfo.avatar_url) {
							if (!this.$store.getters['user/uploadAvatarTip']) {
								this.$store.commit('user/setUploadAvatarTip', true)
								this.display.avatarChange = true
							}
						}
					} else {
						this.loadingType = 3
						this.loadingText = err.data.msg || '加载失败'
					}
				})
			},
			onAvatarClose() {
				this.display.avatarChange = false
			},
			onAvatarChange(avatarUrl) {
				this.userInfo.avatar_url = avatarUrl
			},
			onJumpOrder() {
				uni.switchTab({
					url: '/pages/gift/order'
				})
			},
			onJumpRecord() {
				uni.navigateTo({
					url: '/pages/home/record'
				})
			},
			onJumpAll() {
				uni.navigateTo({
					url: '/pages/gift/list'
				})
			},
			onJumpGift(giftId) {
				uni.navigateTo({
					url: '/pages/gift/detail?gift_id=' + giftId
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: env(safe-area-inset-bottom);
		background: $pq-bg-color;
	}

	.wrap {}

	.user-data {
		padding: 0 40upx 40upx 40upx;
		display: flex;
		flex-direction: column;
		background: #fff;
		background-image: url('@/static/imgs/home_top_bg.png');
		background-repeat: no-repeat;
		background-size: cover;
	}

	.user-box {
		margin-top: 200upx;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.user-info {
			display: flex;
			align-items: center;
			justify-content: space-between;

			.b-avatar {
				width: 140upx;
				height: 140upx;

				image {
					width: 140upx;
					height: 140upx;
					border-radius: 50%;
				}
			}

			.b-info {
				margin-left: 30upx;
				flex: 1;
				display: flex;
				flex-direction: column;

				.s-name {
					font-size: 32upx;
					color: #fff;
				}

				.s-org {
					margin-top: 30upx;
					padding: 0 20upx;
					width: fit-content;
					background: #fff;
					border-radius: 10upx;
					color: #333;
					line-height: 20px;
					height: 20px;
					font-size: 24upx;
					text-align: center;
				}
			}
		}

		.b-action {
			background: #FBC315;
			color: #fff;
			font-size: 28upx;
			padding: 0 40upx;
			height: 80upx;
			line-height: 80upx;
			border-radius: 40upx;
		}
	}

	.fund-box {
		margin-top: 40upx;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.b-fund {
			display: flex;
			align-items: baseline;

			.s-tit {
				font-size: 24upx;
				color: #eee;
			}

			.s-num {
				margin: 0 20upx;
				font-size: 60upx;
				font-weight: bold;
				color: #fff;
			}

			.s-unit {
				font-size: 24upx;
				color: #eee;
			}

		}

		.b-action {
			border: 2upx solid #fff;
			color: #fff;
			font-size: 28upx;
			padding: 0 40upx;
			height: 80upx;
			line-height: 80upx;
			border-radius: 40upx;
		}
	}

	.hot-gifts {
		padding: 30upx;
		margin-top: 30upx;
		background: #fff;

		.b-column {
			font-size: 30upx;
		}

		.b-items {
			margin-top: 30upx;
			white-space: nowrap;

			.b-item {
				width: 160upx;
				margin-right: 30upx;
				display: inline-block;

				&:last-child {
					margin-right: 0;
				}

				.b-main {
					display: flex;
					flex-direction: column;
					align-items: center;
				}

				.b-image {
					width: 160upx;
					height: 160upx;
					position: relative;

					image {
						width: 160upx;
						height: 160upx;
					}

					.s-gain {
						position: absolute;
						left: 0;
						bottom: 0;
						background: rgba(0, 0, 0, 0.5);
						padding: 0 10upx;
						width: 140upx;
						height: 40upx;
						text-align: center;
						font-size: 20upx;
						color: #fff;
					}
				}

				.s-title {
					margin-top: 10upx;
					font-size: 24upx;
					width: 160upx;
					text-align: center;
				}

				.s-fund {
					margin-top: 10upx;
					font-size: 28upx;
					color: #DA5650;
				}

				.s-action {
					margin-top: 10upx;
					background: #DA5650;
					padding: 0 30upx;
					height: 60upx;
					line-height: 60upx;
					font-size: 24upx;
					color: #fff;
					border-radius: 30upx;

					&.f-disable {
						background: rgba(218, 86, 80, 0.8)
					}
				}
			}
		}
	}

	.all-gifts {
		margin-top: 30upx;
		background: #fff;

		.b-column {
			padding: 30upx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.s-tit {
				font-size: 30upx;
				color: #333;
			}

			.s-more {
				display: flex;
				align-items: center;
				font-size: 28upx;
				color: #666;

				.iconfont {
					margin-left: 10upx;
				}
			}
		}

		.b-items {
			margin-top: -30upx;
			.b-item {
				padding: 30upx;
				border-bottom: 1upx solid #eee;
				display: flex;

				&:last-child {
					border-bottom: none;
				}

				.b-left {
					width: 160upx;
					height: 160upx;

					image {
						width: 160upx;
						height: 160upx;
					}
				}

				.b-main {
					margin-left: 30upx;
					flex: 1;
					display: flex;
					flex-direction: column;

					.b-title {
						font-size: 28upx;
						color: #333;
						width: 500upx;
					}

					.b-center {
						margin-top: 20upx;
						display: flex;
						justify-content: space-between;

						.b-fund {
							display: flex;
							align-items: baseline;

							.s-num {
								font-size: 32upx;
								color: #DA5650;
							}

							.s-worth {
								margin-left: 30upx;
								font-size: 24upx;
								color: #999;
							}
						}

						.b-action {
							.s-action {
								background: #DA5650;
								color: #fff;
								font-size: 24upx;
								padding: 0 30upx;
								border-radius: 30upx;
								height: 60upx;
								line-height: 60upx;

								&.f-disable {
									background: rgba(218, 86, 80, 0.8)
								}
							}
						}
					}

					.b-amount {
						margin-top: 10upx;
						display: flex;
						justify-content: space-between;

						.s-left {
							font-size: 24upx;
							color: #999;
						}

						.s-gain {
							font-size: 24upx;
							color: #999;
						}
					}
				}
			}
		}
	}
</style>
